/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlets;

import beans.SeekerCustomer;
import beans.SeekerFriend;
import beans.SeekerImgTag;
import beans.SeekerUploadImg;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 *
 * @author Dhruv
 */

//view a mutual friend's profile page
public class ViewProfileAction extends Action {

    //constructor
    public ViewProfileAction(SeekerCustomer model) {
        super(model);
    }

    //identifies the operation name
    public String getName() {
        return "ViewProfile";
    }

    public Object perform(HttpServletRequest req, HttpServletResponse resp) {

        //JSP page to dispatch the request to
        String address = "";
        //use the session object to get the model object
        HttpSession session = req.getSession();
        model = (SeekerCustomer)session.getAttribute("session1");

        //if you click on your own name on your friends profile, go to your home page
        if(req.getParameter("mutFriend").equals(model.getName()))
        {
            address = "ValidateUser.jsp";

            //get user name
            String myName = model.getName();
            //get user's picture
            String myPicture = model.getPicture();
            //get all friend requests to this user
            ArrayList<SeekerFriend> friendRequests = model.getFriendRequests();
            //get all the tags the friends added AFTER they became mutual friends
            ArrayList<SeekerImgTag> allFriendsTags = model.getAllFriendsTags();
            //get all the images the friends added AFTER they became mutual friends
            ArrayList<SeekerUploadImg> allFriendsImages = model.getAllFriendsImages();

            //store attribute(s) in this request, which is(are) reset between requests
            req.setAttribute("allFriendsImages", allFriendsImages);
            req.setAttribute("myName", myName);
            req.setAttribute("myPicture", myPicture);
            req.setAttribute("friendRequests", friendRequests);
            req.setAttribute("allFriendsTags", allFriendsTags);
        }
        //else if you click on a mutualFriends friend who is not your mutualFriend,
        //you cannot view his/her profile
        else if(!model.isMutualFriend(model.getName(), req.getParameter("mutFriend"))){
            address = "Error.jsp";
            //store attribute(s) in this request, which is(are) reset between requests
            req.setAttribute("error", "You cannot view this profile since you are"
                    + " not mutual friends with this person.");            
        }
        //else you can view that persons profile
        else{
            address = "ViewProfile.jsp";
            //get the mutual friend customer object
            SeekerCustomer mutFriend = SeekerCustomer.getCustomer(req.getParameter("mutFriend"));

            //get mutual friend's name
            String friendName = mutFriend.getName();
            //get mutual friend's profile picture
            String friendProfilePic = mutFriend.getPicture();
            //get mutual friend's images
            ArrayList<SeekerUploadImg> friendsImgs = mutFriend.getMyImages();
            //get mutual friend's tags
            ArrayList<String> friendsTags = mutFriend.getMyTags();
            //get mutual friend's friends
            ArrayList<String> friendsFriends = mutFriend.getMutualFriends();

            //store attribute(s) in this request, which is(are) reset between requests
            req.setAttribute("friendName", friendName);
            req.setAttribute("friendProfilePic", friendProfilePic);
            req.setAttribute("friendsImgs", friendsImgs);
            req.setAttribute("friendsTags", friendsTags);
            req.setAttribute("friendsFriends", friendsFriends);
        }       

        //below object receives requests from the client and sends them to JSP address
        RequestDispatcher dispatcher = req.getRequestDispatcher(address);
        try {
            dispatcher.forward(req, resp);
        } catch (ServletException ex) {
            Logger.getLogger(CreateUserAction.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
              Logger.getLogger(CreateUserAction.class.getName()).log(Level.SEVERE, null, ex);
        }

        //returning the object conforms to the Abstract Action class definition
        //object needed to maintain session between requests
        return model;
    }
}